package com.author.surface;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextPane;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.LineBorder;
import javax.swing.plaf.basic.BasicSliderUI;
import javax.swing.plaf.metal.MetalSliderUI;

public final class TextPanel extends JPanel {
	/** Color of the Text Panel */
	public static final Color	TEXTPANEL_COLOR		= Color.LIGHT_GRAY;

	/* The width and the height of the Text Panel */
	private static int			TEXTPANEL_WIDTH		= 0;
	private static int			TEXTPANEL_HEIGHT	= 0;

	/*
	 * The Text Panel contains the Canvas Panel inside which the Text Pane is
	 * located. The Canvas is white background colored. The Canvas Panel will
	 * also have a compound border effect
	 */
	private JPanel				canvasPanel;

	/* The width and the height of the Canvas Panel */
	private static int			CANVASPANEL_WIDTH	= 0;
	private static int			CANVASPANEL_HEIGHT	= 0;

	/* Color of the Canvas Panel */
	private static final Color	CANVASPANEL_COLOR	= Color.LIGHT_GRAY;

	/*
	 * The Text Panel contains two panels including the canvas panel. The canvas
	 * panel is put in the middle, whereas Panel1 sits at the top
	 */
	private JPanel				Panel1;

	/** Slider just above the Canvas Panel */
	private static JSlider		slider;
	/** Height of the slider above the Canvas Panel */
	private static final int	SLIDER_HEIGHT		= 25;

	/** Text Pane */
	public final TextPane		textPane			= new TextPane();

	protected TextPanel(int width, int height) throws Exception {
		/*
		 * Set the size of the Text Panel which sits at the middle of the
		 * application surface in between Tool Panel and Status Panel. The
		 * Application surface frame will be responsible for setting the height
		 * and the width of the middle Text Panel
		 */
		TextPanel.TEXTPANEL_WIDTH = width;
		TextPanel.TEXTPANEL_HEIGHT = (int) height;
		setSize(TextPanel.TEXTPANEL_WIDTH, TextPanel.TEXTPANEL_HEIGHT);
		setPreferredSize(getSize());

		/* Set background color of the Text Panel and its layout */
		setBackground(TEXTPANEL_COLOR);
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		/* Add Panel1 at the top of the Text Panel */
		Panel1 = new JPanel();
		Panel1.setSize(new Dimension(getWidth(),
				(SystemProperty.SCREEN_HEIGHT / 20)));
		Panel1.setPreferredSize(Panel1.getSize());
		Panel1.setBackground(TEXTPANEL_COLOR);
		Panel1.setVisible(true);
		add(Panel1);

		/* Add a slider to the top panel */
		slider = new CustomSlider();

		/* Create a new slider UI for responding to mouse events */
		BasicSliderUI extSliderUI = new ExtendedSliderUI(slider);
		slider.setUI(extSliderUI);
		slider.setSize(new Dimension(TextPanel.TEXTPANEL_WIDTH,
				TextPanel.SLIDER_HEIGHT));
		slider.setPreferredSize(slider.getSize());
		Panel1.add(slider, BorderLayout.NORTH);

		/* Add canvasPanel at the middle of the Text Panel */
		canvasPanel = new JPanel();
		TextPanel.CANVASPANEL_WIDTH = getWidth();
		TextPanel.CANVASPANEL_HEIGHT = (int) getHeight();
		canvasPanel.setSize(CANVASPANEL_WIDTH, CANVASPANEL_HEIGHT);
		// canvasPanel.setPreferredSize(canvasPanel.getSize());

		/* Set layout and background color of the Canvas Panel */
		canvasPanel.setLayout(new FlowLayout());
		canvasPanel.setBackground(CANVASPANEL_COLOR);
		canvasPanel.setVisible(true);
		add(canvasPanel);

		/*
		 * Make the slider aware of the underlying text panel on which it needs
		 * to draw the ruler line when the user presses the thumb of the slider
		 */
		((ExtendedSliderUI) extSliderUI).setPanelForSlider(this);

		/* Add the text pane to the canvas panel */
		canvasPanel.add(textPane);

		/* Make Text Panel visible */
		setPreferredSize(new Dimension(getWidth(), getHeight()));
		setVisible(true);

		/* Make the custom slider aware of the text pane */
		// ((CustomSlider) slider).setTextPane(c.getTextPane());
	}

	/**
	 * The Canvas pane will be placed inside the Canvas Panel. The Canvas Pane
	 * will have the Text Pane inside it. The Canvas Pane will also have a
	 * compound border with a shadow effect.
	 */
	protected class Canvas extends JPanel {
		/** The width and the height of the Canvas Pane is fixed */
		public static final int	CANVASPANE_WIDTH	= 695;
		public static final int	CANVASPANE_HEIGHT	= 1680;

		/** Text Pane of the author software */
		private JTextPane		textPane;

		/** Canvas Pane color */
		public final Color		CANVASPANE_COLOR	= Color.WHITE;

		protected Canvas() {
			/* Set size of the Canvas Pane */
			setSize(new Dimension(CANVASPANE_WIDTH, CANVASPANE_HEIGHT));
			setPreferredSize(getSize());
			setBackground(CANVASPANE_COLOR);

			// Layout of the Canvas Pane
			setLayout(null);

			// Add Text Pane to the Canvas Pane
			addTextPane();

			// Make Canvas Pane visible
			setVisible(true);
		}

		/**
		 * Adds the Text Pane to the Canvas Pane present inside the Canvas Panel
		 * of the Text Panel. The complete component hierarchy is as follows,
		 * 
		 * Surface (JFrame) -> TextPanel (JPanel) -> CanvasPanel (JPanel) ->
		 * CanvasPane (JPanel) -> TextPane (JTextPane)
		 */
		private void addTextPane() {
			textPane = new TextPane();
			add(textPane);
		}

		/**
		 * @return TextPane inside the Canvas panel
		 */
		public JTextPane getTextPane() {
			return this.textPane;
		}
	}

	/**
	 * @return Canvas panel inside the text panel
	 */
	public JPanel getCanvasPanel() {
		return this.canvasPanel;
	}

	/**
	 * @return The JSlider above the text pane
	 */
	public static JSlider getSlider() {
		return TextPanel.slider;
	}

	/**
	 * @return The text pane inside the canvas panel
	 */
	public JTextPane getTextPane() {
		return this.textPane;
	}
}
